
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

import java.util.Arrays;

public class WordCountApp {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder()
                .appName("WordCountApp")
                .master("local[*]")  //本地运行模式
                .getOrCreate();

        Dataset<Row> data = spark.read().text("/Users/wangxi/code/spark_demo/src/main/resources/input.txt");
        Dataset<Row> wordCounts = data
                .flatMap((FlatMapFunction<Row,String>) row -> Arrays.asList(row.getString(0).split(" ")).iterator(), Encoders.STRING())
                .groupBy("value")
                .count();

        wordCounts.show();

        spark.stop();


    }
}
